package com.star.links;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.widget.Toast;


import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.star.links.Service.ISettingFragmentCallBack;


public class MainActivity extends AppCompatActivity {

    GameFragment gameFragment;
    ExplainFragment explainFragment;
    SettingFragment settingFragment;
    private FragmentManager manager;
    private FragmentTransaction transaction;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        gameFragment=new GameFragment();
        explainFragment=new ExplainFragment();
        settingFragment=new SettingFragment();
        manager = getSupportFragmentManager();
        transaction = manager.beginTransaction();
        transaction.add(R.id.gameFragment,gameFragment);
        transaction.commit();

        settingFragment.setISettingFragmentCallBack(new ISettingFragmentCallBack() {
            @Override
            public void sendMsgToActivity(int Msg) {
                Bundle bundle=new Bundle();
                bundle.putInt("difficultyLevel",Msg);
                gameFragment.setArguments(bundle);
                Log.e("star", String.valueOf(Msg));
            }
        });

        BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.bottomNavigationView);
        navigation.getMenu().getItem(1).setChecked(true);//设置默认选中的Tab
        navigation.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                transaction=manager.beginTransaction();
                /*通过判断进行选择Tab之后想要的操作，一般是进行fragment的动态切换*/
                switch (item.getItemId()) {
                    case R.id.gameDescription:
                        transaction.replace(R.id.gameFragment,explainFragment);
                        transaction.commitNow();
                        return true;
                    case R.id.startGame:
                        transaction.replace(R.id.gameFragment,gameFragment);
                        transaction.commitNow();
                        return true;
                    case R.id.sysSetting:
                        transaction.replace(R.id.gameFragment,settingFragment);
                        transaction.commitNow();
                        return true;
                }
                return false;
            }
        });
    }




}